/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI.Components;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 *
 * @author Nguyen Hong Phu (1142066) - 11HCB (HCMUS)
 */
public final class Toolbar extends javax.swing.JPanel implements ActionListener{
    
    private boolean isAdding;
    private boolean isUpdating;
    
    /**
     * Creates new form Toolbar
     */
    public Toolbar() {
        initComponents();
        // Đăng ký sự kiện cho các control
        btnAdd.addActionListener(this);
        btnEdit.addActionListener(this);
        btnCancel.addActionListener(this);
        btnClose.addActionListener(this);
        btnDelete.addActionListener(this);
        btnSave.addActionListener(this);
        btnRefresh.addActionListener(this);
        
        setStateControl(false);
    }
    
    /**
     * Enable controls if true else disable
     * @param isEditing 
     */
    public void setStateControl(boolean isEditing)
    {
        btnAdd.setEnabled(!isEditing);
        btnEdit.setEnabled(!isEditing);
        btnDelete.setEnabled(!isEditing);
        
        btnSave.setEnabled(isEditing);
        btnCancel.setEnabled(isEditing);
        
        btnRefresh.setEnabled(!isEditing);
    }
    
    //<editor-fold defaultstate="collapsed" desc="Ẩn hiện các control">
    private boolean allowAdd;
    private boolean allowEdit;
    private boolean allowSave;
    private boolean allowDelete;
    private boolean allowRefresh;
    private boolean allowCancel;
    private boolean allowClose;
    
    /**
     * @return the allowAdd
     */
    public boolean isAllowAdd() {
        return allowAdd;
    }
    
    /**
     * @param allowAdd the allowAdd to set
     */
    public void setAllowAdd(boolean allowAdd) {
        btnAdd.setVisible(allowAdd);
    }
    
    /**
     * @return the allowEdit
     */
    public boolean isAllowEdit() {
        return allowEdit;
    }
    
    /**
     * @param allowEdit the allowEdit to set
     */
    public void setAllowEdit(boolean allowEdit) {
        btnEdit.setVisible(allowAdd);
    }
    
    /**
     * @return the allowSave
     */
    public boolean isAllowSave() {
        return allowSave;
    }
    
    /**
     * @param allowSave the allowSave to set
     */
    public void setAllowSave(boolean allowSave) {
        btnSave.setVisible(allowAdd);
    }
    
    /**
     * @return the allowDelete
     */
    public boolean isAllowDelete() {
        return allowDelete;
    }
    
    /**
     * @param allowDelete the allowDelete to set
     */
    public void setAllowDelete(boolean allowDelete) {
        btnDelete.setVisible(allowAdd);
    }
    
    /**
     * @return the allowRefresh
     */
    public boolean isAllowRefresh() {
        return allowRefresh;
    }
    
    /**
     * @param allowRefresh the allowRefresh to set
     */
    public void setAllowRefresh(boolean allowRefresh) {
        btnRefresh.setVisible(allowAdd);
    }
    
    /**
     * @return the allowCancel
     */
    public boolean isAllowCancel() {
        return allowCancel;
    }
    
    /**
     * @param allowCancel the allowCancel to set
     */
    public void setAllowCancel(boolean allowCancel) {
        btnCancel.setVisible(allowAdd);
    }
    
    /**
     * @return the allowClose
     */
    public boolean isAllowClose() {
        return allowClose;
    }
    
    /**
     * @param allowClose the allowClose to set
     */
    public void setAllowClose(boolean allowClose) {
        btnClose.setVisible(allowAdd);
    }
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="Sử dụng và bắt sự kiện click trên các button">
    
    public void addEventPerformClick(IToolbarPerformEvent e){
        this.listenerList.add(IToolbarPerformEvent.class, e);
    }
    @Override
    public final void actionPerformed(ActionEvent e) {
        Object[] listener = listenerList.getListenerList();
        ToolbarEvent stevEvent;
        for (int i = 0; i < listener.length; i+=2) {
             // Moi 1 event gom 2 thanh phan: class event và instance event
            if(listener[i] == IToolbarPerformEvent.class){
                stevEvent = new ToolbarEvent();
                if(e.getSource() == btnAdd){
                     isAdding = true;
                     isUpdating = false;
                     stevEvent.setIsAdding(true);
                    ((IToolbarPerformEvent)listener[i+1]).performEventAdd(stevEvent);
                     setStateControl(!stevEvent.isIsError());
                }else if(e.getSource() == btnEdit){
                     isAdding = false;
                     isUpdating = true;
                     stevEvent.setIsUpdating(true);
                    ((IToolbarPerformEvent)listener[i+1]).performEventEdit(stevEvent);
                    setStateControl(!stevEvent.isIsError());
                }else if(e.getSource() == btnCancel){
                    ((IToolbarPerformEvent)listener[i+1]).performEventCancel(stevEvent);
                     setStateControl(stevEvent.isIsError());
                }else if(e.getSource() == btnClose){
                    ((IToolbarPerformEvent)listener[i+1]).performEventClose(stevEvent);
                }else if(e.getSource() == btnDelete){
                    stevEvent = new ToolbarEvent();
                    ((IToolbarPerformEvent)listener[i+1]).performEventDelete(stevEvent);
                }else if(e.getSource() == btnSave){
                      stevEvent.setIsUpdating(isUpdating);
                      stevEvent.setIsAdding(isAdding);
                     ((IToolbarPerformEvent)listener[i+1]).performEventSave(stevEvent);
                     setStateControl(stevEvent.isIsError());
                }else if(e.getSource() == btnRefresh){
                    ((IToolbarPerformEvent)listener[i+1]).performEventRefresh(stevEvent);
                }
            }
        }
    }
    //</editor-fold>
    
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar = new javax.swing.JToolBar();
        btnAdd = new javax.swing.JButton();
        btnEdit = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        btnSave = new javax.swing.JButton();
        btnRefresh = new javax.swing.JButton();
        jSeparator3 = new javax.swing.JToolBar.Separator();
        btnCancel = new javax.swing.JButton();
        btnClose = new javax.swing.JButton();

        jToolBar.setRollover(true);

        btnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/add.png"))); // NOI18N
        btnAdd.setToolTipText("Thêm");
        btnAdd.setFocusable(false);
        btnAdd.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnAdd.setMargin(new java.awt.Insets(2, 2, 2, 2));
        btnAdd.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar.add(btnAdd);

        btnEdit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/edit.png"))); // NOI18N
        btnEdit.setToolTipText("Sửa");
        btnEdit.setFocusable(false);
        btnEdit.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnEdit.setMargin(new java.awt.Insets(2, 2, 2, 2));
        btnEdit.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar.add(btnEdit);

        btnDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/delete.png"))); // NOI18N
        btnDelete.setToolTipText("Xóa");
        btnDelete.setFocusable(false);
        btnDelete.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnDelete.setMargin(new java.awt.Insets(2, 2, 2, 2));
        btnDelete.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar.add(btnDelete);
        jToolBar.add(jSeparator1);

        btnSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/save.png"))); // NOI18N
        btnSave.setToolTipText("Lưu");
        btnSave.setFocusable(false);
        btnSave.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnSave.setMargin(new java.awt.Insets(2, 2, 2, 2));
        btnSave.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar.add(btnSave);

        btnRefresh.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/refresh.png"))); // NOI18N
        btnRefresh.setToolTipText("Cập nhật");
        btnRefresh.setFocusable(false);
        btnRefresh.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnRefresh.setMargin(new java.awt.Insets(2, 2, 2, 2));
        btnRefresh.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar.add(btnRefresh);
        jToolBar.add(jSeparator3);

        btnCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/cancel.png"))); // NOI18N
        btnCancel.setToolTipText("Hủy");
        btnCancel.setFocusable(false);
        btnCancel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnCancel.setMargin(new java.awt.Insets(2, 2, 2, 2));
        btnCancel.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar.add(btnCancel);

        btnClose.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/close.png"))); // NOI18N
        btnClose.setToolTipText("Đóng");
        btnClose.setFocusable(false);
        btnClose.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnClose.setMargin(new java.awt.Insets(2, 2, 2, 2));
        btnClose.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar.add(btnClose);

        add(jToolBar);
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnClose;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton btnRefresh;
    private javax.swing.JButton btnSave;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JToolBar.Separator jSeparator3;
    private javax.swing.JToolBar jToolBar;
    // End of variables declaration//GEN-END:variables

  


    
}
